package com.woniu.inlet.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.woniu.command.Command;
import com.woniu.command.appcommand.AddAppCommand;
import com.woniu.command.appcommand.DelAppCommand;
import com.woniu.command.appcommand.UpdateAppCommand;
import com.woniu.inlet.vo.ApplicationDetailVo;
import com.woniu.outlet.po.ApplicationDetail;
import com.woniu.service.ApplicationService;
import com.woniu.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("ApplicationDetails")
@Api(tags = "申请物料controller")
public class ApplicationDetailController {
    @Autowired
    private ApplicationService applicationService;

    /** 查询所有的申请的用料
     * @return
     *
     */
    @ApiOperation("查询所有的申请的用料")
    @GetMapping("getApplicationDetails")
    public ResponseResult getApplicationDetails() throws JsonProcessingException {
        List<ApplicationDetail> applicationDetailList = applicationService.getApplicationDetailList();
        return copyCode(applicationDetailList);
    }


    /** 根据请领的id查询该清领单下所有的物料
     * @param metId
     * @return
     */
    @ApiOperation("根据请领的id查询该请领单下所有的物料")
    @GetMapping("getApplicationDetailsByMetId")
    public ResponseResult getApplicationDetailsByMetId( Integer metId){
        List<ApplicationDetail> applicationDetail = applicationService.getApplicationDetail(metId);
        return copyCode(applicationDetail);
    }


//    @PostMapping("addApplicationDetail")
//    @ApiOperation("添加请领物料")
//    public ResponseEntity<String> addApplicationDetail(@RequestBody ApplicationDetail applicationDetail){
//        Command command = new AddAppCommand();
//        command.execute(applicationDetail);
//        return new ResponseEntity<>("Ok",HttpStatus.OK);
//    }

    @DeleteMapping("delApplicationDetail")
    @ApiOperation("删除请领物料")
    public ResponseResult delApplicationDetail(Integer id){
        Command command = new DelAppCommand();
        command.execute(id);
        return new ResponseResult(200,"ok");
    }

//    @PutMapping("updateApplicationDetail")
//    @ApiOperation("修改请领物料")
//    public ResponseEntity<String> updateApplicationDetail(@RequestBody ApplicationDetail applicationDetail){
//        Command command = new UpdateAppCommand();
//        command.execute(applicationDetail);
//        return new ResponseEntity<>("OK",HttpStatus.OK);
//    }

    /** 工具方法 将Po集合转换成Vo集合并输出
     * @param applicationDetailList
     * @return
     */
    private ResponseResult copyCode (List<ApplicationDetail> applicationDetailList) {
        ArrayList<ApplicationDetailVo> applicationDetailVos = new ArrayList<>();
        for (ApplicationDetail applicationDetail : applicationDetailList) {
            ApplicationDetailVo applicationDetailVo = new ApplicationDetailVo();
            BeanUtils.copyProperties(applicationDetail,applicationDetailVo);
            applicationDetailVos.add(applicationDetailVo);
        }
        return new ResponseResult(applicationDetailVos,"查询成功",200 );
    }



}